package com.zpz.framework.zpzoauth.common.extend;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class JsonExtend {

    static final ObjectMapper _objectMapper = new ObjectMapper();

    static {
        SetObjectMapper(_objectMapper);
//		//日期格式化
//		_objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//		//序列化非 null 字段
//		_objectMapper.setSerializationInclusion(Include.NON_NULL);
//		//忽略未知属性
//		_objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//		//大小写原样输出
//		//@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY, getterVisibility=JsonAutoDetect.Visibility.NONE)
//		_objectMapper.setVisibility(PropertyAccessor.FIELD,JsonAutoDetect.Visibility.ANY);
//		_objectMapper.setVisibility(PropertyAccessor.GETTER,JsonAutoDetect.Visibility.NONE);
    }

    ;

    /**
     * 设置json转换格式
     *
     * @param mapper
     * @author zhangpanzhi
     */
    public static void SetObjectMapper(ObjectMapper mapper) {
        //日期格式化
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        //序列化非 null 字段
        mapper.setSerializationInclusion(Include.NON_NULL);
        //忽略未知属性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //大小写原样输出
        //@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY, getterVisibility=JsonAutoDetect.Visibility.NONE)
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);

    }

    /**
     * 将对象转换为 json 格式
     *
     * @param data
     * @return
     */
    public static String ToJson(Object data) {
        if (data == null)
            return null;
        try {
            String resultStr = _objectMapper.writeValueAsString(data);
            return resultStr;
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将对象转换为 json 格式(使用格式化输出，消耗性能)
     *
     * @param data
     * @return
     */
    public static String ToJsonFormat(Object data) {
        try {
            String resultStr = _objectMapper.writerWithDefaultPrettyPrinter()
                    .writeValueAsString(data);
            // System.out.println(resultStr);
            return resultStr;
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json 反序列化
     *
     * @param jsonString 待转换json
     * @param type       类型
     * @return
     */
    public static <T> T ToObject(String jsonString, Class<T> type) {
        if (StringExtend.isNullOrEmpty(jsonString))
            return null;
        try {
            T result = (T) _objectMapper.readValue(jsonString, type);
            return result;
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json 反序列化 list
     *
     * @param jsonString        待转换json
     * @param jsonTypeReference 类型
     * @return
     */
    public static <T> T ToObject(String jsonString, TypeReference<T> jsonTypeReference) {
        try {
            T result = (T) _objectMapper.readValue(jsonString, jsonTypeReference);
            return result;
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
